package com.open.party.cloud

import android.Manifest
import android.app.ActivityManager
import android.content.Context
import android.os.Process
import com.sinothk.android.utils.XUtils
import com.sinothk.rxretrofit.RxRetrofit
import com.sinothk.video.core.VideoApplication
import com.tencent.bugly.crashreport.CrashReport
import com.xinstall.XInstall


class MainApplication : VideoApplication() {

    companion object {

        val permissions = arrayOf(
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.CALL_PHONE,
            Manifest.permission.READ_PHONE_STATE
        )

        fun newInstance(): MainApplication {
            return MainApplication()
        }
    }

    override fun onCreate() {
        super.onCreate()
        XUtils.init(this)
        if (isMainProcess()) {
            XInstall.init(this)//XInstall.setDebug(f)
            CrashReport.initCrashReport(this, "95254dd70b", false)
            RxRetrofit.doAuth(resources.getString(R.string.init))
        }
    }

    private fun isMainProcess(): Boolean {
        val pid = Process.myPid()
        val activityManager: ActivityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        for (appProcess in activityManager.runningAppProcesses) {
            if (appProcess.pid == pid) {
                return applicationInfo.packageName == appProcess.processName
            }
        }
        return false
    }
}